package com.tianji.exam.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianji.api.dto.exam.QuestionBizDTO;
import com.tianji.api.dto.exam.QuestionDTO;
import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.common.exceptions.BadRequestException;
import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.CollUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.exam.domain.po.Exam;
import com.tianji.exam.domain.po.ExamDetails;
import com.tianji.exam.domain.po.QuestionDetail;
import com.tianji.exam.domain.vo.ExamDetailVO;
import com.tianji.exam.domain.vo.ExamPageVO;
import com.tianji.exam.domain.vo.QuestionDetailVO;
import com.tianji.exam.mapper.ExamMapper;
import com.tianji.exam.service.IExamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianji.exam.service.IQuestionBizService;
import com.tianji.exam.service.IQuestionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 考试表 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-04-26
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IExamService {

    private final IQuestionBizService questionBizService;

    private final IQuestionService questionService;

    /**
     * 分页查询考试记录
     * @param query
     * @return
     */
    @Override
    public PageDTO<ExamPageVO> queryPageExam(PageQuery query) {
        //查询当前用户id
        Long userId = UserContext.getUser();
        //分页查询
        Page<Exam> page = lambdaQuery().
                eq(Exam::getUserId, userId).
                page(query.toMpPage("commit_time", false));
        //拿取数据
        List<Exam> records = page.getRecords();
        //为空判断
        if (CollUtils.isEmpty(records)){
            return PageDTO.empty(page);
        }
        //封装为VO
        List<ExamPageVO> list=new ArrayList<>(records.size());
        for (Exam r : records) {
            //拷贝属性
            ExamPageVO examPageVO = BeanUtils.copyBean(r, ExamPageVO.class);
            //存入list
            list.add(examPageVO);
        }
        return PageDTO.of(page,list);
    }

    /**
     * 根据id查询具体考试详情
     * @param id
     * @return
     */
    @Override
    public ExamDetailVO queryExamDetailById(Long id) {
        //根据考试记录id查询
        Exam exam = lambdaQuery()
                .eq(Exam::getId, id)
                .one();

        if (exam==null){
            throw new BadRequestException("无数据!");
        }
        //属性拷贝
        ExamDetailVO examDetailVO = BeanUtils.copyBean(exam, ExamDetailVO.class);
        //补全数据
        Long examZjId = exam.getExamZjId();
        //查询问题详情
        List<QuestionBizDTO> bizDTOS = questionBizService.queryQuestionIdsByBizId(examZjId);
        if (CollUtils.isEmpty(bizDTOS)){
            log.error("数据为空");
        }
        List<QuestionDTO> question=new ArrayList<>();
        //获取到题目问题id集合
        for (QuestionBizDTO bizDTO : bizDTOS) {
            Long questionId = bizDTO.getQuestionId();
            QuestionDetailVO questionDetailVO = questionService.queryQuestionDetailById(questionId);
            QuestionDTO questionDTO = BeanUtils.copyBean(questionDetailVO, QuestionDTO.class);
            question.add(questionDTO);
        }
        examDetailVO.setQuestion(question);
        return examDetailVO;
    }
}
